package com.bryantkobe.myspringcode.config;

import com.bryantkobe.myspringcode.advice.GlobalResponseAdvice;
import com.bryantkobe.myspringcode.exception.massert.BusinessAssert;
import com.bryantkobe.myspringcode.exception.massert.StaticBusinessAssert;
import com.bryantkobe.myspringcode.factory.BusinessExceptionFactory;
import com.bryantkobe.myspringcode.handler.GlobalExceptionHandler;
import com.bryantkobe.myspringcode.properties.ExceptionHandlingProperties;
import com.bryantkobe.myspringcode.service.MExceptionMetricsService;
import com.bryantkobe.myspringcode.service.impl.MExceptionMetricsServiceImpl;
import io.micrometer.core.instrument.MeterRegistry;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @description: 异常配置类
 * @author: alienwarezzl
 * @date: 2025/9/23
 */
@Configuration
@EnableConfigurationProperties(ExceptionHandlingProperties.class)
public class ExceptionHandlingAutoConfiguration {
    @Bean
    @ConditionalOnMissingBean
    public GlobalExceptionHandler globalExceptionHandler() {
        return new GlobalExceptionHandler();
    }

    @Bean
    public GlobalResponseAdvice globalResponseAdvice() {
        return new GlobalResponseAdvice ();
    }

    @Bean
    public MExceptionMetricsService exceptionMetricsService(MeterRegistry meterRegistry) {
        return new MExceptionMetricsServiceImpl(meterRegistry);
    }

    @Bean
    @ConditionalOnMissingBean
    public BusinessExceptionFactory businessExceptionFactory() {
        return new BusinessExceptionFactory();
    }

    @Bean
    @ConditionalOnMissingBean
    public BusinessAssert businessAssert(BusinessExceptionFactory factory) {
        return new BusinessAssert(factory);
    }

    @Bean
    @ConditionalOnMissingBean
    public StaticBusinessAssert staticBusinessAssert(BusinessExceptionFactory factory) {
        return new StaticBusinessAssert(factory);
    }
}
